package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"seat-service/api"
	"seat-service/initialization"
	"seat-service/middleware"
	"seat-service/utils"
)

func Router() {
	g := gin.Default()
	//跨域问题
	g.Use(middleware.Cors())
	//图片资源
	g.Static("./uploads", "./uploads")
	//静态页面
	g.Static("./adminer", "./adminer")
	//路由组声明
	userGroup := userRouter{}
	authGroup := authRouter{}
	menuGroup := menuRouter{}
	dictionaryGroup := dictionaryRouter{}
	messageGroup := messageRouter{}
	carouselGroup := carouselRouter{}
	dataViewGroup := dataViewRouter{}
	reserveGroup := reserveRouter{}
	studentGroup := StudentRouter{}
	buildingGroup := BuildingRouter{}
	classroomGroup := ClassroomRouter{}
	buildingsAdminGroup := buildingsAdminRouter{}
	seatGroup := seatRouter{}
	studentAdminGroup := studentAdminRouter{}
	studentMessageGroup := messageStudentRouter{}
	searchRecordGroup := searchRecordRouter{}
	reservationGroup := ReservationRouter{}
	informationGroup := InformationRouter{}
	classAdminGroup := classroomAdminRouter{}
	availableTimeGroup := AvailableTimeRouter{}
	qrCodeGroup := qrCodeRouter{}
	xcxGroup := XcxRouter{}

	//Casbin
	casbin, err := utils.InitCasbinGorm(initialization.DB)
	if err != nil {
		initialization.SeatLogger.Error(fmt.Sprintf("utils.InitCasbinGorm(initialization.DB) is failed : %v\n", err))
		return
	}
	xcx := g.Group("kywxxcx")
	// 微信小程序接口
	xcxGroup.xcxGroup(xcx)
	//测试
	xcx.GET("test", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"123": "123",
		})
	})
	//后台管理路由组
	group := g.Group("app")
	g.POST("/app/login", api.Login)
	group.Use(middleware.Jwt(), middleware.AuthMiddlewareCasbin(casbin))
	{
		// 后台管理
		//用户路由组
		userGroup.userRouterGroup(group)
		//菜单路由组
		menuGroup.menuRouterGroup(group)
		//字典管理
		dictionaryGroup.dictionaryRouterGroup(group)
		//角色路由组
		authGroup.authRouterGroup(group)

		//座位预约后台
		//消息管理
		messageGroup.messageRouterGroup(group)
		//轮播图管理
		carouselGroup.CarouselRouterGroup(group)
		//数据看板
		dataViewGroup.dataViewRouterGroup(group)
		//楼栋管理
		buildingsAdminGroup.buildingsRouterGroup(group)
		//座位管理
		seatGroup.SeatRouterGroup(group)
		//学生管理
		studentAdminGroup.studentAdminRouterGroup(group)
		//预约管理
		reservationGroup.reservationGroup(group)
		//教室管理
		classAdminGroup.classroomAdminRouterGroup(group)
		//时间段
		availableTimeGroup.availableTimeAdminGroup(group)
		//生成二维码
		qrCodeGroup.qrCodeRouterGroup(group)
	}

	app := g.Group("app")
	app.POST("student/login", studentApi.Login)
	app.GET("student/wxLogin", studentApi.WxLogin)
	app.Use(middleware.Jwt())
	{
		// 学生
		studentGroup.studentGroup(app)
		// 预约
		reserveGroup.reserveGroup(app)
		// 教学楼
		buildingGroup.buildingGroup(app)
		// 信息
		studentMessageGroup.studentMessageRouterGroup(app)
		// 教室
		classroomGroup.classroomGroup(app)
		// 搜索记录
		searchRecordGroup.searchRecordGroup(app)
		// 个人信息
		informationGroup.informationGroup(app)
		// 获取轮播图
		carouselGroup.AppRouterGroup(app)
		// 可选时间段
		availableTimeGroup.availableTimeGroup(app)
	}

	err = g.Run(":8080")
	if err != nil {
		initialization.SeatLogger.Error(fmt.Sprintf("gin start is failed : %v\n", err))
		return
	}
}
